package m.store.task;

import m.base.model.LevelInfo;
import m.base.model.StoreInfo;
import m.base.service.LevelInfoService;
import m.base.service.StoreInfoService;
import m.base.service.UserInfoService;
import m.base.util.LevelUtil;
import m.base.util.PowerUtil;
import m.common.action.Action;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.store.service.RepositoryInfoService;
import m.system.RuntimeData;
import m.system.util.DateUtil;

import java.util.Date;
import java.util.List;

public class TaskRun extends m.system.SystemTaskRun{
    /**
     * 每天0点调用
     */
    @Override
    public void run(boolean b) throws Exception {
        Date date=new Date();
        LevelInfo level = RuntimeData.getService(LevelInfoService.class).getDefLevel();
        List<StoreInfo> ss = ModelQueryList.getModelList(StoreInfo.class, new String[]{"oid"}, null,
                QueryCondition.lt("levelEndDate", date));//lt小于
        for(StoreInfo store : ss){
            try {
                RuntimeData.getService(StoreInfoService.class).setLevel(store.getOid(),level.getOid(), DateUtil.getAddMonth(date,12));
                RuntimeData.getService(UserInfoService.class).limitUserNum(store.getOid(),level.getUserNum());
                RuntimeData.getService(RepositoryInfoService.class).limitRepositoryNum(store.getOid(), level.getRepositoryNum());
            }catch (Exception e){
                Action.directLog("定时任务","更新商户级别失败",this.getClass().getName(),store.getOid(),e);
            }
        }
        ss = ModelQueryList.getModelList(StoreInfo.class, new String[]{"oid"}, null,
            QueryCondition.lt("logoutDate", date));//lt小于
        for(StoreInfo store : ss){
            try {
                RuntimeData.getService(StoreInfoService.class).deleteAllData(store.getOid());
            }catch (Exception e){
                Action.directLog("定时任务","注销商户数据失败",this.getClass().getName(),store.getOid(),e);
            }
        }
    }

}
